package com.bestom.communication.sql;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * @author shulman
 */
public class DbManage {

    protected static DbManage _instance;
    private SQLiteDatabase db;

    public DbManage(Context context){
        db = new DbHelper(context).getWritableDatabase();
    }

    public static DbManage getInstance(Context context){
        if (_instance == null) _instance = new DbManage(context);
        return _instance;
    }

    public void addRecord(String table, String topic){
        db.execSQL("insert into " + table + "(topic) values(?)", new String[] { topic });
    }

    public void deleteData(String table, String topic){
        db.execSQL("delete from " + table + " where topic = ?",new String[]{ topic });
    }

    public void update(String table, String oldTopic, String newTopic){
        db.execSQL("update " + table + " set topic = ? where topic = ?",new String[]{ newTopic, oldTopic });
    }

    /**
     * 功能   降序查询数据表
     */
    public Cursor select(String tableName){
        return db.rawQuery("select * from " + tableName + " order by _id desc",null);
    }

}
